package com.dingxin.ship_together_api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.dingxin.ship_together_api.service.SysUserService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Author: ctj
 * Date: 2022-08-03
 * Time: 14:21
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;


    // @RequestMapping("login")
    // public SaResult doLogin(String username, String password, String verifyCode, HttpServletRequest request) {
    //     // if (!sysUserService.checkVerifyCode(verifyCode, request)) {
    //     //     return SaResult.error("验证码错误");
    //     // }
    //     if (sysUserService.checkLogin(username, password)) {
    //         return SaResult.ok("登录成功");
    //     }
    //     return SaResult.error("用户名或密码错误");
    // }

    // @GetMapping("getOpenIdByCode")
    // public SaResult getOpenIdByToken(String code){
    //     return SaResult.ok().setData(sysUserService.getOpenIdByCode(code));
    // };

    @GetMapping("login")
    public SaResult getUserPhone(String encryptedData,String iv,String code){
        return SaResult.ok().setData(sysUserService.login(encryptedData,iv,code));
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @RequestMapping("permission")
    @SaCheckRole("user")
    public String perimission() {
        SaSession session = StpUtil.getSession();
        List<String> role_list = (List<String>)session.get(SaSession.ROLE_LIST);
        SaSession sessionById = SaSessionCustomUtil.getSessionById("role-" + role_list.get(0));
        Set<String> permission_list = (Set<String>)sessionById.get(SaSession.PERMISSION_LIST);
        return "Check User Role And Permission：" + role_list+"Permission List : "+permission_list;
    }

    @GetMapping("getRoleList")
    @SaCheckRole("user")
    public SaResult roleList(){
        return SaResult.ok().setData(StpUtil.getSession().get(SaSession.ROLE_LIST));
    }

    @GetMapping("checkToken")
    @SaCheckLogin
    public SaResult checkToken(){
        return SaResult.ok();
    }

    @RequestMapping("/logout")
    public String logout(){
        StpUtil.getSession().clear();
        StpUtil.logout();
        return "logout";
    }

    @RequestMapping("getInfo")
    public String getInfo(String loginId){
        SaSession session = StpUtil.getSessionByLoginId(loginId);
        Map<String, Object> dataMap = session.getDataMap();
        long timeout = session.getTimeout();
        SaSession currentSession = StpUtil.getSession();
        long timeout1 = currentSession.getTimeout();
        SaSession roleSession = SaSessionCustomUtil.getSessionById("role-role-1");
        Map<String, Object> dataMap2 = roleSession.getDataMap();
        long timeout2 = roleSession.getTimeout();
        return dataMap.toString()+timeout+"\n"+dataMap2.toString()+timeout2+"当前session剩余时长 "+timeout1;
    }
    @RequestMapping("captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CaptchaUtil.out(request, response);
    }

    @RequestMapping("/test")
    public String verify(String verCode, HttpServletRequest request){
        if (!CaptchaUtil.ver(verCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            return "fail";
        }
        return "success";
    }
}
